package com.tumblr.anond2.solr.client.solrj;

import static com.google.common.base.Preconditions.checkNotNull;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.request.ContentStreamUpdateRequest;

/**
 * Solr client for files.
 * <p>XML, JSON, CSV, rich document (e.g., Word, PDF)</p>
 */
public class SolrDOCClient {

	private static SolrDOCClient instance = new SolrDOCClient();
	private static SolrServer server = null;
	
	enum FILE_TYPE { XML, JSON, CSV, RICH_DOCUMENT };
	
	private SolrDOCClient() {
	}

	public static SolrDOCClient getInstance() throws Exception {
		server = checkNotNull(SolrConnection.getSolrServer(), "server not found");
		return instance;
	}

	public void add(File file, FILE_TYPE fileType) throws Exception{
		this.add(null, file, fileType);
	}
	
	public void add(String id, File file, FILE_TYPE fileType) throws Exception{
		String handler = null;
		if ((fileType == FILE_TYPE.XML) ||
			(fileType == FILE_TYPE.JSON) ||
			(fileType == FILE_TYPE.CSV)){
			handler = "/update";
		} else if (fileType == FILE_TYPE.RICH_DOCUMENT){
			handler = "/update/extract";			
		}
		String ctype = mapExtensionToMimeType(file.getName());
		
		ContentStreamUpdateRequest upRequest = new ContentStreamUpdateRequest(handler);
		upRequest.addFile(file, ctype);
		if (id != null){
			upRequest.setParam("literal.id", id);
		}
		server.request(upRequest);
		server.commit(false, false, true);
	}
	
	  public static String mapExtensionToMimeType(String fileName)
	  {
	    int slashIndex = fileName.lastIndexOf("/");
	    if (slashIndex != -1)
	      fileName = fileName.substring(slashIndex+1);
	    int dotIndex = fileName.lastIndexOf(".");
	    if (dotIndex == -1)
	      return null;
	    return ExtensionMimeMap.mapToMimeType(fileName.substring(dotIndex+1).toLowerCase(java.util.Locale.ROOT));
	  }	
  
}

class ExtensionMimeMap
{
  protected final static Map<String,String> mimeMap;
  static {
    mimeMap = new HashMap<String,String>();
    mimeMap.put("xml", "text/xml");
    mimeMap.put("csv", "text/csv");
    mimeMap.put("json", "application/json");
    mimeMap.put("pdf", "application/pdf");
    mimeMap.put("rtf", "text/rtf");
    mimeMap.put("html", "text/html");
    mimeMap.put("htm", "text/html");
    mimeMap.put("doc", "application/msword");
    mimeMap.put("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
    mimeMap.put("ppt", "application/vnd.ms-powerpoint");
    mimeMap.put("pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation");
    mimeMap.put("xls", "application/vnd.ms-excel");
    mimeMap.put("xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    mimeMap.put("odt", "application/vnd.oasis.opendocument.text");
    mimeMap.put("ott", "application/vnd.oasis.opendocument.text");
    mimeMap.put("odp", "application/vnd.oasis.opendocument.presentation");
    mimeMap.put("otp", "application/vnd.oasis.opendocument.presentation");
    mimeMap.put("ods", "application/vnd.oasis.opendocument.spreadsheet");
    mimeMap.put("ots", "application/vnd.oasis.opendocument.spreadsheet");
    mimeMap.put("txt", "text/plain");
    mimeMap.put("log", "text/plain");
  }

  public static String mapToMimeType(String extension)
  {
    return mimeMap.get(extension.toLowerCase(java.util.Locale.ROOT));
  }
  
}